package cn.anecansaitin.whimsy.mixin.common;

import cn.anecansaitin.whimsy.common.register.ModEffects;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.entity.LivingEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.function.Predicate;

/**
 * 这里是为了让拥有MeteorStrike Buff的实体不再与其他实体相碰撞
 */
@Mixin(LivingEntity.class)
public abstract class LevelEntityMixin {
    @Unique
    private final Predicate<Entity> whimsy$noMeteorStrikeEffect = entity -> !(entity instanceof LivingEntity living) || !living.hasEffect(ModEffects.METEOR_STRIKE.get());
    @Unique
    private final Predicate<Entity> whimsy$alwaysFalse = entity -> false;

    //修改碰撞实体的检测，剔除掉拥有MeteorStrike效果的实体
    @Redirect(method = "pushEntities", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/EntitySelector;pushableBy(Lnet/minecraft/world/entity/Entity;)Ljava/util/function/Predicate;"))
    public Predicate<Entity> redirect$getEntities(Entity pEntity) {
        if (((LivingEntity) (Object)this).hasEffect(ModEffects.METEOR_STRIKE.get())) {
            return whimsy$alwaysFalse;
        }

        return EntitySelector.pushableBy((Entity) (Object)this).and(whimsy$noMeteorStrikeEffect);
    }

    //修改是否会被敌对生物视作敌人
    @Inject(method = "canBeSeenAsEnemy", at = @At(value = "HEAD"), cancellable = true)
    public void inject$canBeSeenAsEnemy(CallbackInfoReturnable<Boolean> cir) {
        if (((LivingEntity)(Object)this).hasEffect(ModEffects.METEOR_STRIKE.get())) {
            cir.setReturnValue(false);
        }
    }
}
